package com.file.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.entity.Customer;
import com.entity.Sysdic;
import com.file.entity.FileCustomer;
import com.file.entity.FileMeter;
import com.file.service.CustomerService;
import com.setting.entity.Children;
import com.setting.service.OrgService;
import com.setting.service.SysdicService;
import com.utils.JsonResult;
import com.utils.JsonSysdic;
import com.utils.Utils;

/**
 * @Title: CustomerController.java
 * @Package com.controller
 * @author 孙梦楠
 * @date 2017年7月24日 上午10:23:14
 * @version V1.0.0
 */

@RestController
@RequestMapping(value = "/file/customer")
public class CustomerController {

	@Autowired
	private CustomerService customerservice;

	@Autowired
	private SysdicService sysdicservice;

	@Autowired
	private OrgService orgservice;

	private static final Logger log = LoggerFactory.getLogger(CustomerController.class);

	// 条件查询
	@RequestMapping(value = "", method = RequestMethod.POST)
	public JsonResult<List<FileCustomer>> listCustomerByConditions(

			@RequestBody Customer customer,
			// @RequestBody Children org,
			@CookieValue(value = "opeorgcookie", required = false) String opeorgcookie,
			@CookieValue(value = "opetypecookie", required = false) String opetypecookie,
			@CookieValue(value = "languagecookie", required = false) String languagecookie) {

		String customeraccount = customer.getCustomeraccount();
		String customername = customer.getCustomername();
		String customeraddress = customer.getCustomeraddress();
		String customertype = opetypecookie;
		String customerstatus = customer.getCustomerstatus();
		String orgvalue = customer.getCustomerphone();
		String status = customer.getCustomerwechat();

		//
		// String orgvalue = org.getOrgvalue();
		// String status = org.getStatus();
		//
		// log.info("customeraccount "+customeraccount);
		// log.info("customername "+customername);
		// log.info("customeraddress "+customeraddress);
		// log.info("customertype "+customertype);
		// log.info("opeorgcookie "+opeorgcookie);
		// log.info("customerstatus "+customerstatus);
		// log.info("customerphone "+customerphone);
		// log.info("customerwechat "+customerwechat);

		if (orgvalue != null && (status.equals("1") || status.equals("2"))) {
			return orgservice.listCustomerByOrg(orgvalue, languagecookie, status,customertype);
		} else {
			return customerservice.listCustomerByConditions(customeraccount, customername, customeraddress,
					customertype, opeorgcookie, customerstatus, null, null, languagecookie);
		}
	}

	// 字典查询
	@RequestMapping(value = "/listsysdicCustomertype", method = RequestMethod.GET)
	public JsonResult<String> listsysdicCustomertype(
			@CookieValue(value = "languagecookie", required = false) String languagecookie,
			@CookieValue(value = "opetypecookie", required = false) String opetypecookie) {

		return customerservice.listcustomertype(languagecookie, opetypecookie);
	}

	@RequestMapping(value = "/listsysdicCustomerstatus", method = RequestMethod.GET)
	public JsonResult<List<JsonSysdic>> listsysdicCustomerstatus(
			@CookieValue(value = "languagecookie", required = false) String languagecookie) {

		String sysdictype = "0018";
		Integer state = 230;
		String Message = "Customerstatus字段列表";

		return sysdicservice.listsysdic(sysdictype, state, Message, languagecookie);
	}

	// 增加
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public JsonResult<Customer> addcustomer(@RequestBody Customer customer,
			@CookieValue(value = "opeorgcookie", required = false) String opeorgcookie,
			@CookieValue(value = "opetypecookie", required = false) String opetypecookie) {


		String customername = customer.getCustomername();
		String customeraddress = customer.getCustomeraddress();
		String customertype = opetypecookie;
		String customerphone = customer.getCustomerphone();
		String customerwechat = customer.getCustomerwechat();

		return customerservice.addcustomer( customername, customeraddress, customertype, opeorgcookie,
				customerphone, customerwechat);

	}

	// 删除
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public JsonResult<Customer> deletecollector(@RequestBody Customer customer) {

		Integer customerid = customer.getCustomerid();
		log.info(customerid.toString());
		return customerservice.deletecustomer(customerid);

	}

	// 修改页面
	@RequestMapping(value = "/getbyid", method = RequestMethod.POST)
	public JsonResult<FileCustomer> updatecollector(@RequestBody Customer customer,
			@CookieValue(value = "languagecookie", required = false) String languagecookie) {

		Integer customerid = customer.getCustomerid();

		// log.info(customerid.toString());

		return customerservice.updateviewcustomer(customerid, languagecookie);

	}

	// 修改
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public JsonResult<Customer> updatecustomer(@RequestBody Customer customer,
			@CookieValue(value = "opeorgcookie", required = false) String opeorgcookie,
			@CookieValue(value = "opetypecookie", required = false) String opetypecookie,
			@CookieValue(value = "languagecookie", required = false) String languagecookie) {
		Integer customerid = customer.getCustomerid();
		String customername = customer.getCustomername();
		String customeraddress = customer.getCustomeraddress();
		String customertype = opetypecookie;
		String customerstatus = null;
		String customerphone = customer.getCustomerphone();
		String customerwechat = customer.getCustomerwechat();

		return customerservice.updatecustomer(customerid, customername, customeraddress, customertype,
				opeorgcookie, customerstatus, customerphone, customerwechat);

	}

	// 根据用户ID查所连表的详细信息
	@RequestMapping(value = "/find_meter_by_customerid", method = RequestMethod.POST)
	public JsonResult<FileMeter> findMeterBycustomerID(@RequestBody Customer customer,
			@CookieValue(value = "languagecookie", required = false) String languagecookie) {
		Integer customerid = customer.getCustomerid();
		return customerservice.findMeterBycustomerID(customerid, languagecookie);
	}

}
